package com.idc.resource.controller;

import com.idc.resource.enums.ErrorCodeEnum;
import com.idc.resource.exception.IdcServiceException;
import com.idc.resource.model.MenuTree;
import com.idc.resource.service.AuthMenuService;
import com.idc.resource.util.LogUtil;
import com.idc.resource.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单控制
 * Created by Lijing on 2017/7/11.
 */
@Slf4j
@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    /** 权限菜单服务 */
    @Autowired
    private AuthMenuService authMenuService;

    /**
     * 用户权限菜单请求
     * @param token 登录令牌
     * @return 权限菜单
     */
    @RequestMapping(value = "/auth/menu/{token}", method = RequestMethod.POST)
    public Result<List<MenuTree>> getMenuTree(@PathVariable("token")String token){
        try {
            LogUtil.updateLogId(null);
            LogUtil.updateUserInfo("",token);
            log.info("收到一个用户权限菜单查询请求，用户token:{}",token);
            return new Result<>(authMenuService.getLoginMenu(token));
        }catch (IdcServiceException ise){
            log.error("获取权限菜单失败,原因:{}",ise.getMessage());
            return new Result<>(ise.getCode(),ise.getMessage());
        }catch (Exception e){
            log.error("获取权限菜单系统异常:",e);
            return new Result<>(ErrorCodeEnum.SYSTEM_ERROR.getCode(),ErrorCodeEnum.SYSTEM_ERROR.getDesc());
        }
    }
}
